package code11_util;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

import org.junit.Test;

public class Test00 {

	@Test
	public void t1() {
		System.out.println("t1------");
		String s = "测试hello";// 当前文件编码 UTF-8
		try {
			byte[] bytes = s.getBytes("UTF-8");
			System.out.println("arr1:"+Arrays.toString(bytes));
			String s2 = new String(bytes, "GBK");
			System.out.println("s2:"+s2);
			byte[] arr2 = s2.getBytes("GBK");
			System.out.println("arr2:"+Arrays.toString(arr2));
			String s3 = new String(arr2, "UTF-8");
			System.out.println("s3="+s3);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	@Test
	public void t2() {
		System.out.println("t2------");
		String s = "测试hello";// 当前文件编码 UTF-8
		try {
			byte[] bytes = s.getBytes("UTF-8");
			System.out.println("arr1:"+Arrays.toString(bytes));
			String s2 = new String(bytes, "ISO-8859-1");
			System.out.println("s2:"+s2);
			byte[] arr2 = s2.getBytes("ISO-8859-1");
			System.out.println("arr2:"+Arrays.toString(arr2));
			String s3 = new String(arr2, "UTF-8");
			System.out.println("s3="+s3);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	@Test
	public void t3() {
		System.out.println("t3------");
		String s = "测试hello";// 当前文件编码 UTF-8
		try {
			byte[] bytes = s.getBytes("GBK");
			System.out.println("arr1:"+Arrays.toString(bytes));
			String s2 = new String(bytes, "ISO-8859-1");
			System.out.println("s2:"+s2);
			byte[] arr2 = s2.getBytes("ISO-8859-1");
			System.out.println("arr2:"+Arrays.toString(arr2));
			String s3 = new String(arr2, "GBK");
			System.out.println("s3="+s3);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	@Test
	public void t4() {
		System.out.println("t4------");
		String s = "测试hello";// 当前文件编码 UTF-8
		try {
			byte[] bytes = s.getBytes("GBK");
			System.out.println("arr1:"+Arrays.toString(bytes));
			String s2 = new String(bytes, "UTF-8");
			System.out.println("s2:"+s2);
			byte[] arr2 = s2.getBytes("UTF-8");
			System.out.println("arr2:"+Arrays.toString(arr2));
			String s3 = new String(arr2, "GBK");
			// 解决失败
			System.out.println("s3="+s3);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	@Test
	public void t5() throws Exception {
		String s = "您好";// 文件编码为 utf-8

		// 1.编码 ——> 正确
		byte[] bytes = s.getBytes("gbk");// 或 utf-8
		System.out.println(Arrays.toString(bytes));

		// 2.解码 ——> 错误
		String str = new String(bytes, "iso-8859-1");// 只能是 iso-8859-1, 如果使用 utf-8 会无法恢复
		System.out.println(str);

		// 3.重新编码
		byte[] bytes2 = str.getBytes("iso-8859-1");
		// 重写编码的字节数组必须和 原始相同, 否则重新编码也是错误的
		System.out.println(Arrays.toString(bytes2));
		// 4.重新解码
		//String str2 = new String(bytes2,"gbk");// 和 1 中 编码格式 相同
		//System.out.println(str2);

		// 3,4 合为一行代码
		String str2 = new String(str.getBytes("iso-8859-1"),"gbk");
		System.out.println(str2);

		// 如果编码不正确，则无论如何解码都没用
		String s1 = "您好";
		bytes = s1.getBytes("iso-8859-1");// 使用 iso-8859-1 编码 不支持中文, 无法正确编码
		str = new String(bytes, "iso-8859-1");
		System.out.println(str);
	}
	
}
